package com.yyairmarkyy.arctool.window;

import android.content.Context;
import android.graphics.Point;
import android.view.WindowManager;


import com.yyairmarkyy.arctool.customview.ArcToolRootLyt;
import com.yyairmarkyy.arctool.inter.ArcOnKeyLisitener;
import com.yyairmarkyy.arctool.inter.ArcOutsideClickLisitener;
import com.yyairmarkyy.arctool.inter.CenterViewClickListener;
import com.yyairmarkyy.arctool.item.ArcFadeItem;
import com.yyairmarkyy.arctool.item.ArcLevelItem;
import com.yyairmarkyy.arctool.item.AiyouArcMenuItem;

import java.util.List;


/**
 * Created by aiyou on 2017/10/18.
 */

public class ArcToolWindow {


    private ArcToolController controller;


    public ArcToolWindow(Context context, WindowManager windowManager, WindowManager.LayoutParams layoutParams) {
        controller = new ArcToolController(context,this,windowManager,layoutParams);
    }


    /**
     * 利用windowmanager展示布局
     */
    public void show(){
        controller.show();
    }


    public void dismss(){
        controller.dismiss();
    }


    public boolean isShow(){
        return controller.isShow;
    }

    /**
     *更改总半径大小
     * @param maxRadius
     */
    public void updateMaxRadius(int maxRadius){
        controller.setMaxRadius(maxRadius);
    }

    /**
     * 更改总背景颜色
     * @param color
     */
    public void updateBackgroudColor(int color){
        controller.setBgColor(color);
    }


    public void updateCenterViewIconResid(int iconResid, int size,Point position){
        controller.setCenterViewIconResid(iconResid,size,position);
    }


    /**
     * 隐藏recycleview
     * @param index 数组索引
     */
    public void hideLevelItem(int index){
        controller.hideRecyclerView(index);
    }

    /**
     * 显示recycleview
     * @param index 数组索引
     */
    public void showLevelItem(int index){
        controller.showRecyclerView(index);
    }


    /**
     * 设置分割线的现实状态
     * @param index 数组索引
     * @param visiable 显示状态
     */
    public void setFadeItemVisiable(int index,@ArcFadeItem.FadeVisibility int visiable){
        controller.setFadeVisiable(index,visiable);
    }


    /**
     * 获取levelItem 显示状态
     * @param index
     * @return
     */
    public int getLevelVisibility(int index){
        return controller.getLevelItemVisibility(index);
    }

    /**
     * 更新分割线
     * @param fadeItems
     */
    public void updateFadeItems(List<ArcFadeItem> fadeItems){
        controller.setFadeItems(fadeItems);
    }

    /**
     * 更新布局
     */
    public void update(){
        controller.updateRootlyt();
    }

    /**
     * 获取生成好的跟布局
     * @return
     */
    public ArcToolRootLyt getArcRootLyt(){
        return controller.getArcRootLyt();
    }


    public static class Builder{

        private ArcToolController.AiyouArcToolParams p;
        private Context mContext;
        private WindowManager windowManager;
        private WindowManager.LayoutParams layoutParams;

        public Builder(Context context,WindowManager windowManager,WindowManager.LayoutParams layoutParams) {
            this.mContext = context;
            this.windowManager = windowManager;
            this.layoutParams = layoutParams;
            p = new ArcToolController.AiyouArcToolParams(context);
        }


        /**
         * 设置工具总层数(不包括中心层)
         * @param allLevel
         * @return
         */
        public Builder setAllLevel(int allLevel){
            p.allLevel = allLevel;
            return this;
        }


        /**
         * 设置层数中最大层数的半径(也就是说背景最大半径)
         * @param maxRadius
         * @return
         */
        public Builder setMaxRadius(int maxRadius){
            p.maxRadius = maxRadius;
            return this;
        }


        /**
         * 设置中心层背景颜色
         * @param color
         * @return
         */
        public Builder setCenterBackgroundColor(int color){
            p.centerViewBackgroundColor = color;
            return this;
        }


        /**
         * 设置中心层图标
         * @param iconResid
         * @return
         */
        public Builder setCenterViewIconResid(int iconResid, int size,Point position){
            p.centerViewIconResid = iconResid;
            p.centerviewIconSize = size;
            p.centerviewIconPosition = position;
            return this;
        }


        /**
         * 设置中心层半径
         * @param centerViewRadius
         * @return
         */
        public Builder setCenterViewRadius(int centerViewRadius){
            p.centerViewRadius = centerViewRadius;
            return this;
        }


        /**
         * 设置背景颜色
         * @param bgColor
         * @return
         */
        public Builder setBgColor(int bgColor){
            p.bgColor = bgColor;
            return this;
        }

        /**
         * 设置背景透明度
         * @param alpha 0-255
         * @return
         */
        public Builder setBgAlpha(int alpha){
            p.bgAlpha = alpha;
            return this;
        }

        /**
         * 设置每个分割线属性
         * @param fadeItems
         * @return
         */
        public Builder setFadeItems(List<ArcFadeItem> fadeItems){
            p.fadeItems = fadeItems;
            return this;
        }

        /**
         * 设置每一层的属性
         * @param levelItems
         * @return
         */
        public Builder setLevelItems(List<ArcLevelItem> levelItems){
            p.levelItems = levelItems;
            return this;
        }


        public Builder setOutsideClickLisitener(ArcOutsideClickLisitener lisitener){
            p.outsideClickLisitener = lisitener;
            return this;
        }


        public Builder setOnkeyLisitener(ArcOnKeyLisitener lisitener){
            p.onKeyLisitener = lisitener;
            return this;
        }

        /**
         * 设置菜单
         * @param menuItem
         * @return
         */
        public Builder setMenuItem(AiyouArcMenuItem menuItem){
            p.menuItem = menuItem;
            return this;
        }

        public Builder setCenterViewClickListener(CenterViewClickListener centerViewClickListener){
            p.centerViewClickListener = centerViewClickListener;
            return this;
        }


        public ArcToolWindow create(){
            final ArcToolWindow arcToolWindow = new ArcToolWindow(mContext,windowManager,layoutParams);
            p.apply(arcToolWindow.controller);
            if (p.onKeyLisitener == null)
                p.onKeyLisitener = new DefaultOnkeyListener(arcToolWindow);
            return arcToolWindow;
        }




    }

    private static class DefaultOnkeyListener implements ArcOnKeyLisitener{

        ArcToolWindow aiyouArcToolWindow;

        public DefaultOnkeyListener(ArcToolWindow aiyouArcToolWindow) {
            this.aiyouArcToolWindow = aiyouArcToolWindow;
        }

        @Override
        public void onKeyLisitener(int keyCode) {
            aiyouArcToolWindow.dismss();
        }
    }


}
